<template>
  <div>

    <h2>{{ id ? '编辑分类':'新增分类' }}</h2>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-form-item label="名称"  >
        <el-input v-model="model.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit"> 保存</el-button>
      </el-form-item>
    </el-form>

  </div>

</template>

<script>
  export default {
    data() {
      return {
        model:{},
        id:this.$route.query.id
      }

    },
    methods : {
      async save() {
        let res
        if(this.id != null){
          res = await this.$http.post('/cms/category/edit.json/'+this.id, this.model)
        }
        else {
          res = await this.$http.post('/cms/category/add.json', this.model)
        }
        //跳转到list页面
        this.$router.push("/categories/list")
        this.$message({
          type: "success",
          message: '保存成功！'
        })
      },
      async fetch() {
        const  res = await this.$http.get("/cms/category/view.json/"+this.id)
        this.model = res.data.data
      }
    },
    created() {
      this.id && this.fetch()
    }
  }
</script>

<style scoped>

</style>
